package com.jdd.modules.parking.controller;

import com.alibaba.fastjson.JSONObject;
import com.jdd.common.api.vo.Result;
import com.jdd.common.api.vo.ResultVo;
import com.jdd.common.aspect.annotation.AutoLog;

import com.jdd.common.util.RestTemplateUtils;
import com.jdd.modules.order.entity.ParkOrderInfo;
import com.jdd.modules.parking.entity.ParkCarEnterLog;
import com.jdd.modules.parking.mapper.ParkCarEnterLogMapper;
import com.jdd.modules.sdkidentifyresultpushlog.entity.ParkOrderInfoVO;
import com.jdd.modules.sdkidentifyresultpushlog.service.ChargeFeeService;
import com.jdd.modules.sdkidentifyresultpushlog.service.ChargeFeeV2Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * @Description: 停车计费信息
 * @Author: zhaishijie
 * @Date: 2021-04-08
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "停车计费信息")
@RestController
@RequestMapping("/parking/parkCharge")
public class ParkChargeController {
    @Resource
    private ChargeFeeV2Service chargeFeeV2Service;


    @Resource
    private RestTemplateUtils restTemplateUtils;
    @Resource
    private ParkCarEnterLogMapper parkCarEnterLogMapper;
    /**
     * 停车计费信息-计算停车收费金额
     * @return
     */
    @AutoLog(value = "停车计费信息-计算停车收费金额")
    @ApiOperation(value = "停车计费信息-计算停车收费金额", notes = "停车计费信息-计算停车收费金额")
    @PostMapping(value = "/parkChargeFee")
    public Result<?>  parkChargeFee(@RequestBody ParkOrderInfoVO vo) {
        //拿到入场的车牌
        ParkCarEnterLog parkCarEnterLog = parkCarEnterLogMapper.selectById(vo.getCarEnterId());
        String plateNo=vo.getPlateNo();
        if(null!=parkCarEnterLog){
            plateNo=parkCarEnterLog.getPlateNo();
        }
        //当前车道计费信息，云端缓存，扫描二维码支付
        Result<String>  result = new Result<>();
        //根据停车场编号和车辆类型配置id获取计费规则
        if (StringUtils.isBlank(vo.getEnterTime())){
            return result.error500("车辆入场时间不存在");
        }
        try {
            //计算收费金额,拿到订单信息
            Result<?> chargeFeeAgain = chargeFeeV2Service.chargeFeeAgain(vo.getSerialNo(), plateNo, vo.getCarTypeId());
            ParkOrderInfo parkOrderInfo = new ParkOrderInfo();
            BeanUtils.copyProperties(chargeFeeAgain.getResult(), parkOrderInfo);
            //扫码的时候用出口的
            parkOrderInfo.setPlateNo(vo.getPlateNo());
            log.info("计费信息同步云端:-{}", parkOrderInfo);
            String url = "";            ResponseEntity<ResultVo> post = restTemplateUtils.post(url, parkOrderInfo, ResultVo.class);
            log.info("计费信息同步云端结果:-{}", JSONObject.toJSONString(post));
            return chargeFeeAgain;
        }
        catch (Exception e)
        {
            return result.error500(e.getMessage());
        }
    }
}
